﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var Searchstudents = "select * from Students";
            var sk = StudentsDBHeper.GetSearch(Searchstudents);
            dataGridView1.DataSource = sk;

            //限制数据库的一些限制
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//限制封装
            dataGridView1.ReadOnly = true;//限制编辑
            dataGridView1.AllowUserToAddRows = false;//限制下一行
        }

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var Find = string.Format("select * from Students where Name like '%{0}%'", name);
            var sk = StudentsDBHeper.GetSearch(Find);
            dataGridView1.DataSource = sk;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            EditerForm editerform = new EditerForm();
            var res = editerform.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var Searchstudents = "select * from Students";
                var sk = StudentsDBHeper.GetSearch(Searchstudents);
                dataGridView1.DataSource = sk;
            }
            else
            {
                MessageBox.Show("对不起，您未添加成功，请重新添加", "信息提示框");
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void button3_Click(object sender, EventArgs e)
        {
            var row = dataGridView1.SelectedRows.Count;
            if (row > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["ID"].Value;
                var name = (string)dataGridView1.SelectedRows[0].Cells["Name"].Value;
                var age = (int)dataGridView1.SelectedRows[0].Cells["Age"].Value;
                var sex = (string)dataGridView1.SelectedRows[0].Cells["Sex"].Value;
                var score = (int)dataGridView1.SelectedRows[0].Cells["Score"].Value;
                EditerForm editerform = new EditerForm(id, name, age, sex, score);
                var res = editerform.ShowDialog();
                if (row == 1)
                {
                    var Searchstudent = "select * from Students";
                    var sk = StudentsDBHeper.GetSearch(Searchstudent);
                    dataGridView1.DataSource = sk;
                }
                else
                {
                    MessageBox.Show("对不起，请您重新选择要更新的对象", "信息提示框");
                }
            }
            else
            {
                MessageBox.Show("对不起，您未选择要更新的对象", "信息提示框");
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            var row = dataGridView1.SelectedRows.Count;
            if (row > 0)
            {
                var id = dataGridView1.SelectedRows[0].Cells["ID"].Value;
                var delete = string.Format("delete from Students where ID = {0}", id);
                StudentsDBHeper.Addordelete(delete);
                if (row == 1)
                {
                    var Searchstudents = "select * from Students";
                    var sk = StudentsDBHeper.GetSearch(Searchstudents);
                    dataGridView1.DataSource = sk;
                    MessageBox.Show("恭喜您，删除成功", "信息提示框");
                }
                else
                {
                    MessageBox.Show("对不起，请您重新选择要删除的对象", "信息提示框");
                }
            }
            else
            {
                MessageBox.Show("对不起，您未选择要删除的对象", "信息提示框");
            }
        }
    }
}
